#include "touch.h"
#include <stdio.h>
#include <unistd.h>
#include <linux/input.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>

void dan_dian(diao *touch_s)
{
    int fd_touch = open("/dev/input/event0", O_RDWR);
    if (fd_touch == -1)
    {
        perror("fd_touch error");
        return;
    }
    touch_s->touch_diao_x = -1;
    touch_s->touch_diao_y = -1;
    struct input_event ev;
    while (1)
    {
        int red = read(fd_touch, &ev, sizeof(ev));
        if (red == -1)
        {
            perror("touch read error");
            close(fd_touch);
            return;
        }
        if (red != sizeof(ev))
        {
            continue;
        }
        if (ev.type == EV_ABS && ev.code == ABS_X)
        {
            touch_s->touch_diao_x = ev.value;
        }
        if (ev.type == EV_ABS && ev.code == ABS_Y)
        {
            touch_s->touch_diao_y = ev.value;
        }
        if (touch_s->touch_diao_x != -1 && touch_s->touch_diao_y != -1)
        {
            break;
        }
    }
    close(fd_touch);
    return;
}